package com.zhanghu.backendZray.interceptor;

import cn.hutool.core.util.StrUtil;
import com.zhanghu.backendZray.cache.ContextCache;
import com.zhanghu.backendZray.constants.CommonConstant;
import com.zhanghu.backendZray.pojo.dto.UserTokenInfo;
import com.zhanghu.backendZray.service.UserOnlineTokenService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.zhanghu.backendZray.constants.CommonConstant.USER_ID;

@Component
@Slf4j
@RequiredArgsConstructor
public class AsyncHelloInterceptor implements AsyncHandlerInterceptor {
    private final UserOnlineTokenService userOnlineTokenService;

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) {
//        log.info("AsyncHandlerInterceptor :" + Thread.currentThread().getName() + "---afterConcurrentHandlingStarted-->" + request.getRequestURI());
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//        System.err.println("interceptor preHandle...");
//        log.error("preHandle start...");
        String zToken = request.getHeader(CommonConstant.TOKEN);
        if (StrUtil.isNotEmpty(zToken)) {
            // 判断token是否有效、是否过期
            /*boolean effect = JwtUtil.verifyTokenExpireTime(zToken);
            if (effect) {
                // 设置上下文
                ContextCache.setCurrentUser(JwtUtil.getUserFromToken(zToken));
            }*/
            UserTokenInfo userTokenInfo = userOnlineTokenService.getUserTokenInfoByToken(zToken);
            ContextCache.setCurrentUser(userTokenInfo);
            if (userTokenInfo != null && userTokenInfo.getId() != null) {
                MDC.put(USER_ID, userTokenInfo.getId().toString());
            }
        } else {
            // 设置上下文
            ContextCache.setCurrentUser(null);
        }
//        log.error("preHandle end... :" + Thread.currentThread().getName() + "---preHandle-->" + request.getRequestURI());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
//        System.err.println("interceptor postHandle...");
//        log.info("AsyncHandlerInterceptor :" + Thread.currentThread().getName() + "---postHandle-->" + request.getRequestURI());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//        System.err.println("interceptor afterCompletion...");
//        log.info("AsyncHandlerInterceptor :" + Thread.currentThread().getName() + "---afterCompletion-->" + request.getRequestURI());
//        ContextCache.removeCurrentUser();
    }
}